package com.atguigu.tingshu.album.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.config.VodConstantProperties;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.GuiLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.common.util.UploadFileUtil;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcloud.vod.VodUploadClient;
import com.qcloud.vod.model.VodUploadRequest;
import com.qcloud.vod.model.VodUploadResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Tag(name = "声音管理")
@RestController
@RequestMapping("/api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;
	@Autowired
	private VodConstantProperties vodConstantProperties;

	/**
	 * 上传声音
	 * @param file
	 * @return
	 */

	@SneakyThrows
	@PostMapping("/uploadTrack")
	public Result uploadTrack( MultipartFile file){

//		VodConstantProperties要从ICO容器里面注入，直接NEW获取不到NACOS上的值的
		//用户上传到本地文件夹,
		String tempPath = UploadFileUtil.uploadTempPath(vodConstantProperties.getTempPath(), file);
		//初始化云点播客户端
		VodUploadClient client = new VodUploadClient(vodConstantProperties.getSecretId() ,vodConstantProperties.getSecretKey());
		//构建上传请求
		VodUploadRequest request = new VodUploadRequest();
		request.setMediaFilePath(tempPath);

//		request.setMediaType(file.getContentType());
		//将本地文件上传到腾讯云
		VodUploadResponse response = client.upload(vodConstantProperties.getRegion(), request);
		//获取上传文件的唯一表示
		String fileId = response.getFileId();
		//返回coverurl
		String coverUrl = response.getCoverUrl();
		//返回trackTitle
		//获取音频播放地址
		String mediaUrl = response.getMediaUrl();
		//创建一个json对象，返回文件唯一标识和播放地址
		JSONObject result = new JSONObject();
		result.put("mediaFileId",fileId);
		result.put("mediaUrl",mediaUrl);
		return Result.ok(result);
	}

	/**
	 * 保存声音
	 * @param trackInfoVo
	 * @return
	 */
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	/**
	 * 分页功能
	 * @param trackInfoQuery
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiLogin
	@PostMapping(value = "/findUserTrackPage/{page}/{limit}")
	public Result <Page<TrackListVo>> findUserTrackPage(@RequestBody TrackInfoQuery trackInfoQuery,
									@PathVariable(value = "page") Integer page,
									@PathVariable(value = "limit") Integer limit){
		Page<TrackListVo> pageInfo = new Page<>(page,limit);
		return Result.ok(trackInfoService.findUserTrackPage(trackInfoQuery,pageInfo));
	}

	/**
	 * 删除声音
	 * @param trackId
	 * @return
	 */
	@GuiLogin
	@DeleteMapping("/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable(value = "trackId") Integer trackId){
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

	/**
	 * 声音回显
	 * @param trackId
	 * @return
	 */
	@GuiLogin
	@GetMapping("/getTrackInfo/{trackId}")
	public Result getTrackInfo(@PathVariable(value = "trackId")Long trackId){
		return Result.ok(trackInfoService.getOne(new LambdaQueryWrapper<TrackInfo>()
				.eq(TrackInfo::getUserId, AuthContextHolder.getUserId())
				.eq(TrackInfo::getId,trackId)));
	}

	/**
	 * 修改
	 * @param trackId
	 * @param trackInfoVo
	 * @return
	 */
	@GuiLogin
	@PutMapping("/updateTrackInfo/{trackId}")
	public Result updateTrackInfo (@PathVariable(value = "trackId")Long trackId,@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(trackInfoVo,trackId);
		return Result.ok();

	}


	/**
	 * 展示专辑详情
	 * @param albumId
	 * @param size
	 * @param page
	 * @return
	 */
	@GuiLogin
	@GetMapping("/findAlbumTrackPage/{albumId}/{page}/{size}")
	public Result findAlbumTrackPage(@PathVariable(value = "albumId") Long albumId,
	@PathVariable(value = "size") Long size,@PathVariable(value = "page")Long page){
		Page<AlbumTrackListVo> objectPage =  new Page<>(page,size);
		trackInfoService.findAlbumTrackPage(objectPage, albumId);
		return Result.ok(objectPage);
	}

	/**
	 * 获取声音统计信息
	 * @param trackId
	 * @return
	 */
	@GetMapping("getTrackStatVo/{trackId}")
	public Result getTrackStatVo(@PathVariable(value = "trackId")Long trackId){
		return Result.ok(trackInfoService.getTrackStatVo(trackId));
	}

	/**
	 * 查询用户购买声音列表
	 * @param trackId
	 * @return
	 */
	@GetMapping("/findUserTrackPaidList/{trackId}")
	public Result  findUserTrackPaidList(@PathVariable(value = "trackId")Long trackId){
		return Result.ok(trackInfoService.findUserTrackPaidList(trackId));
	}
}

